package com.ssyx;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * ClassName:CompletableFutureDemo1
 * Description:
 *
 * @Author kay
 * @Create 2024/5/17 10:57
 * @Version 1.0
 */
//线程串行化方法
public class CompletableFutureDemo4 {
    public static void main(String[] args) {
        //创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(3);

        //任务1执行结果：100
        CompletableFuture<Integer> futureA = CompletableFuture.supplyAsync(() ->{
            int result = 1024;
            System.out.println("任务1:" + result);
            return result;
        },executorService);

        // 任务2 获取到任务1执行的结果 方法thenApplyAsync
        CompletableFuture<Integer> futureB = futureA.thenApplyAsync((result) ->{
            System.out.println("任务2:"+result);
            return result;
        },executorService);

        //任务3: 无法获取futureA返回结果 方法thenRunAsync
        CompletableFuture<Void> futureC = futureA.thenRunAsync(() -> {
            System.out.println("任务3,无法获取结果,继续往下执行...");
        }, executorService);
    }
}
